package cz.masa.FotoCAT;

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.*;

class SQLCon{
    private static Connection con;
    
    public SQLCon(){
    }

    public static Connection getConnection(){
        return con;
    }
    
    public static int connect(String url, String driver, String usr, String pwd){
        try {
            Class.forName(driver);
        } catch(java.lang.ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Driver error: " + e.getMessage(), "Chyba", JOptionPane.ERROR_MESSAGE);
            return -1;
        }
        try {
            SQLCon.con = (Connection)DriverManager.getConnection(url, usr, pwd);
            //System.out.println(con.get);
        } catch(SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Chyba", JOptionPane.ERROR_MESSAGE);
            return -1;
        }
        return 1;
    }    
    
    public static void exSQL(String sql){
        try {
            Statement stmt = (Statement)SQLCon.con.createStatement();
            stmt.executeUpdate(sql);
        } catch(SQLException ex) {
            System.err.println("==> SQLException: ");
            while (ex != null){
                System.out.println("Message:   " + ex.getMessage ());
                System.out.println("SQLState:  " + ex.getSQLState ());
                System.out.println("ErrorCode: " + ex.getErrorCode ());
                ex = ex.getNextException();
                System.out.println("");
            }
        }
    }
	
    public static ResultSet exSQLres(String sql){
        ResultSet rs;
        try {
            Statement stmt = (Statement)SQLCon.con.createStatement();
            rs = (ResultSet)stmt.executeQuery(sql);
            //System.out.println(sql);
        } catch(SQLException ex) {
            System.err.println("==> SQLException: ");
            while (ex != null){
                System.out.println("Message:   " + ex.getMessage ());
                System.out.println("SQLState:  " + ex.getSQLState ());
                System.out.println("ErrorCode: " + ex.getErrorCode ());
                ex = ex.getNextException();
                System.out.println("");
            }
            rs = null;
        }
        return rs;
    }
	
}